package com.hsfw.pmbasicdata.utils;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.hsfw.pmbasicdata.service.BasicDataService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * 主要提供系统初始化信息统计与预加载操作
 */
@Component
public class SingleVolBasicData {
    private final Logger log = LoggerFactory.getLogger(SingleVolBasicData.class);

    @Autowired
    BasicDataService initBasicDataService;
    private static BasicDataService basicDataService;
    //基础数据树形菜单缓存
    private List<HashMap<String, Object>> baseTreeMenuList;
    //部队编制树形菜单缓存
    private List<HashMap<String, Object>> armyTreeMenuList;
    //部署数据树形菜单缓存
    private List<HashMap<String, Object>> deployTreeMenuList;
    private String baseTreeType="base";
    private String armyTreeType="army";
    private String deployTreeType="deploy";

    // 组件的表头缓存
    public static HashMap<String, List> componentTableHead;
    static {
        componentTableHead = new HashMap<>();
        componentTableHead.put("sensor", JSONArray.parseArray("[" +
                "{" +
                "type: \"index\"," +
                "label: \"序号\"" +
                "}," +
                "{" +
                "prop:\"name\"," +
                "label:\"名称\"" +
                "}," +
                "{" +
                "prop:\"type\"," +
                "label:\"类型\"" +
                "}," +
                "{" +
                "prop:\"rangemax\"," +
                "label:\"最大作用距离(公里)\"" +
                "}" +
                "]"));
        componentTableHead.put("comm", JSONArray.parseArray("[" +
                "{" +
                "type: \"index\"," +
                "label: \"序号\"" +
                "}," +
                "{" +
                "prop:\"parentspecific\"," +
                "label:\"通信支持协同作战\"" +
                "}," +
                "{" +
                "prop:\"name\"," +
                "label:\"名称\"" +
                "}," +
                "{" +
                "prop:\"type\"," +
                "label:\"类型\"" +
                "}," +
                "{" +
                "prop:\"comments\"," +
                "label:\"注释\"" +
                "}," +
                "{" +
                "prop:\"range\"," +
                "label:\"范围(公里)\"" +
                "}," +
                "{" +
                "prop:\"channels\"," +
                "label:\"频道(个)\"" +
                "}" +
                "]"));
        componentTableHead.put("mount", JSONArray.parseArray("[" +
                "{" +
                "type: \"index\"," +
                "label: \"序号\"" +
                "}," +
                "{" +
                "prop:\"name\"," +
                "label:\"名称\"" +
                "}," +
                "{" +
                "prop:\"comments\"," +
                "label:\"注释\"" +
                "}" +
                "]"));
        componentTableHead.put("loadout", JSONArray.parseArray("[" +
                "{" +
                "type: \"index\"," +
                "label: \"序号\"" +
                "}," +
                "{" +
                "prop:\"name\"," +
                "label:\"名称\"" +
                "}," +
                "{" +
                "prop:\"comments\"," +
                "label:\"注释\"" +
                "}," +
                "{" +
                "prop:\"loadoutrole\"," +
                "label:\"挂载方案的作用\"" +
                "}," +
                "{" +
                "prop:\"defaultmissionprofile\"," +
                "label:\"默认任务剖面\"" +
                "}" +
                "]"));
        componentTableHead.put("signature", JSONArray.parseArray("[" +
                "{" +
                "type: \"index\"," +
                "label: \"序号\"" +
                "}," +
                "{" +
                "prop:\"type\"," +
                "label:\"类型\"" +
                "}," +
                "{" +
                "prop:\"front\"," +
                "label:\"正面\"" +
                "}," +
                "{" +
                "prop:\"side\"," +
                "label:\"侧面\"" +
                "}," +
                "{" +
                "prop:\"rear\"," +
                "label:\"后面\"" +
                "}," +
                "{" +
                "prop:\"top\"," +
                "label:\"上面\"" +
                "}" +
                "]"));
        componentTableHead.put("weaponsignature", JSONArray.parseArray("[" +
                "{" +
                "type: \"index\"," +
                "label: \"序号\"" +
                "}," +
                "{" +
                "prop:\"type\"," +
                "label:\"类型\"" +
                "}," +
                "{" +
                "prop:\"front\"," +
                "label:\"正面\"" +
                "}," +
                "{" +
                "prop:\"side\"," +
                "label:\"侧面\"" +
                "}," +
                "{" +
                "prop:\"rear\"," +
                "label:\"后面\"" +
                "}" +
                "]"));
        componentTableHead.put("code", JSONArray.parseArray("[" +
                "{" +
                "type: \"index\"," +
                "label: \"序号\"" +
                "}," +
                "{" +
                "prop:\"description\"," +
                "label:\"属性\"" +
                "}" +
                "]"));
        componentTableHead.put("propulsion", JSONArray.parseArray("[" +
                "{" +
                "type: \"index\"," +
                "label: \"序号\"" +
                "}," +
                "{" +
                "prop:\"name\"," +
                "label:\"名称\"" +
                "}," +
                "{" +
                "prop:\"type\"," +
                "label:\"类型\"" +
                "}," +
                "{" +
                "prop:\"numberofengines\"," +
                "label:\"发动机数量（台）\"" +
                "}" +
                "]"));
        componentTableHead.put("fuel", JSONArray.parseArray("[" +
                "{" +
                "type: \"index\"," +
                "label: \"序号\"" +
                "}," +
                "{" +
                "prop:\"type\"," +
                "label:\"燃油类型\"" +
                "}," +
                "{" +
                "prop:\"capacity\"," +
                "label:\"燃料容量(飞机-公斤，武器-秒，其它（电池-分钟，其它-公斤）)\"" +
                "}" +
                "]"));
        componentTableHead.put("aircraftfacilities", JSONArray.parseArray("[" +
                "{" +
                "type: \"index\"," +
                "label: \"序号\"" +
                "}," +
                "{" +
                "prop:\"type\"," +
                "label:\"类型\"" +
                "}," +
                "{" +
                "prop:\"physicalsize\"," +
                "label:\"物理尺寸级别\"" +
                "}," +
                "{" +
                "prop:\"capacity\"," +
                "label:\"容量\"" +
                "}," +
                "{" +
                "prop:\"runwaylength\"," +
                "label:\"跑道长度(米)\"" +
                "}" +
                "]"));
        componentTableHead.put("magazine", JSONArray.parseArray("[" +
                "{" +
                "type: \"index\"," +
                "label: \"序号\"" +
                "}," +
                "{" +
                "prop:\"name\"," +
                "label:\"名称\"" +
                "}," +
                "{" +
                "prop:\"comments\"," +
                "label:\"注释\"" +
                "}," +
                "{" +
                "prop:\"capacity\"," +
                "label:\"容量\"" +
                "}" +
                "]"));
        componentTableHead.put("warhead", JSONArray.parseArray("[" +
                "{" +
                "type: \"index\"," +
                "label: \"序号\"" +
                "}," +
                "{" +
                "prop:\"name\"," +
                "label:\"名称\"" +
                "}," +
                "{" +
                "prop:\"type\"," +
                "label:\"类型\"" +
                "}" +
                "]"));
        componentTableHead.put("target", JSONArray.parseArray("[" +
                "{" +
                "type: \"index\"," +
                "label: \"序号\"" +
                "}," +
                "{" +
                "prop:\"type\"," +
                "label:\"目标类型\"" +
                "}" +
                "]"));
        componentTableHead.put("dockingfacilities", JSONArray.parseArray("[" +
                "{" +
                "type: \"index\"," +
                "label: \"序号\"" +
                "}," +
                "{" +
                "prop:\"type\"," +
                "label:\"类型\"" +
                "}," +
                "{" +
                "prop:\"physicalsize\"," +
                "label:\"物理尺寸级别\"" +
                "}," +
                "{" +
                "prop:\"capacity\"," +
                "label:\"容量\"" +
                "}" +
                "]"));
        componentTableHead.put("wra", JSONArray.parseArray("[" +
                "{" +
                "type: \"index\"," +
                "label: \"序号\"" +
                "}," +
                "{" +
                "prop:\"codeiddescription\"," +
                "label:\"目标类型\"" +
                "}," +
                "{" +
                "prop:\"weaponqtydescription\"," +
                "label:\"武器齐射数\"" +
                "}," +
                "{" +
                "prop:\"shooterqtydescription\"," +
                "label:\"齐射最大发射单元数\"" +
                "}," +
                "{" +
                "prop:\"selfdefencerangedescription\"," +
                "label:\"自防御距离\"" +
                "}" +
                "]"));
        componentTableHead.put("weapondamageability", JSONArray.parseArray("[" +
                "{" +
                "type: \"index\"," +
                "label: \"序号\"" +
                "}," +
                "{" +
                "prop:\"rcsdescription\"," +
                "label:\"典型RCS\"" +
                "}," +
                "{" +
                "prop:\"speeddescription\"," +
                "label:\"典型速度\"" +
                "}," +
                "{" +
                "prop:\"heightdescription\"," +
                "label:\"典型高度\"" +
                "}," +
                "{" +
                "prop:\"maxrange\"," +
                "label:\"远界(公里)\"" +
                "}," +
                "{" +
                "prop:\"minrange\"," +
                "label:\"近界(公里)\"" +
                "}," +
                "{" +
                "prop:\"maxheight\"," +
                "label:\"高界(米)\"" +
                "}," +
                "{" +
                "prop:\"minheight\"," +
                "label:\"低界(米)\"" +
                "}," +
                "{" +
                "prop:\"maxapproachpoint\"," +
                "label:\"最大航捷(公里)\"" +
                "}," +
                "{" +
                "prop:\"maxfilghtpathangle\"," +
                "label:\"最大航路角(度)\"" +
                "}," +
                "{" +
                "prop:\"maxhighlowangle\"," +
                "label:\"最大高低角(度)\"" +
                "}," +
                "{" +
                "prop:\"averagespeed\"," +
                "label:\"弹药平均速度(m/s)\"" +
                "}," +
                "{" +
                "prop:\"ammunitionnum\"," +
                "label:\"弹药数量(枚)\"" +
                "}" +
                "]"));
    }

    @PostConstruct
    public void init() {
        this.basicDataService = initBasicDataService;

    }


    private volatile static SingleVolBasicData singleVolDictionary;


    private SingleVolBasicData() {
    }

    public static SingleVolBasicData getInstance() {
        if (singleVolDictionary == null) {
            synchronized (SingleVolBasicData.class) {
                if (singleVolDictionary == null) {
                    singleVolDictionary = new SingleVolBasicData();
                }
            }
        }
        return singleVolDictionary;
    }

    public List<HashMap<String, Object>> gettreeMenuAll(String treeType) {
        List<HashMap<String, Object>> res=new ArrayList<>();
        if (baseTreeType.equals(treeType)) {
            if (baseTreeMenuList == null) {
                baseTreeMenuList = basicDataService.getTreeMenuByNodeID("-1", "INIT","base");
                res=baseTreeMenuList;
            }else {
                res=baseTreeMenuList;
            }
        }else if (armyTreeType.equals(treeType)) {
            if (armyTreeMenuList == null) {
                armyTreeMenuList = basicDataService.getTreeMenuByNodeID("hsfw-battleframedir-0000000001", "INIT","army");
                res=armyTreeMenuList;
            }else {
                res=armyTreeMenuList;
            }
        }else if (deployTreeType.equals(treeType)) {
            if (deployTreeMenuList == null) {
                deployTreeMenuList = basicDataService.getTreeMenuByNodeID("hsfw-deployfile-00000000000001", "INIT","deploy");
                res=deployTreeMenuList;
            }else {
                res=deployTreeMenuList;
            }
        }
        log.info("已经初始化树形菜单数据");
        return res;
    }

    public List<HashMap<String, Object>> refreshTreeMenuAll(String treeType) {
        List<HashMap<String, Object>> resTree=new ArrayList<>();
        if (baseTreeType.equals(treeType)){
            baseTreeMenuList = basicDataService.getTreeMenuByNodeID("-1", "INIT","base");
            resTree=baseTreeMenuList;
        }else if (armyTreeType.equals(treeType)){
            armyTreeMenuList = basicDataService.getTreeMenuByNodeID("hsfw-battleframedir-0000000001", "INIT","army");
            resTree=armyTreeMenuList;
        }else if (deployTreeType.equals(treeType)){
            deployTreeMenuList = basicDataService.getTreeMenuByNodeID("hsfw-deployfile-00000000000001", "INIT","deploy");
            resTree=deployTreeMenuList;
        }
        log.info("已经刷新树形菜单数据");
        return resTree;
    }

}
